<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cache;
class ProductShow extends Common {
	
	public function index(){
		$id = (int) request()->param('aid');
		$cid = (int) request()->param('cid');
		$cate = Db::name('category')->where(['id'=>$cid])->find();
		if(!isset($cate['template_show'])){
			return $this->fetch(config('template.default_them').'/404');
		}
		
		$this->cacheChange($id);
		$data = db('product')->where(['aid'=>$id])->cache('pshow'.$id,3600)->find();
		
		$front = $this->front($id,$cid);
		$after = $this->after($id,$cid);
		if(!$data){
			return $this->fetch(config('template.default_them').'/404');
		}
		
		// p($data);
		$this->assign('after',$after);
		$this->assign('front',$front);
		$this->assign('content',$data);
		return $this->fetch('template/'.config('template.default_them').'/'.$cate['template_show']);
	}
	
	public function cacheChange($id){
		$str = 'pshow'.$id;
		$c = cache($str);
		if($c){
			$c['click'] = $c['click']+1;
			$data = db('product')->where(['aid'=>$id])->update(['click'=>$c['click']]);
			
			if($data){
				cache($str,$c);
			}
		}
		
	}
	
	public function front($id,$cid){
		$front=db('product')->where("aid",">",$id)->where(['cid'=>$cid])->order('aid asc')->limit('1')->find();
		return $front;
	}
	
	public function after($id,$cid){
		$after=db('product')->where("aid","<",$id)->where(['cid'=>$cid])->order('aid desc')->limit('1')->find();
		return $after;
	}
	
}